<script type="text/javascript">

	/*判断购物车商品数量*/
	/*-----------------*/
	function IsNum(e) {
		var k = window.event ? e.keyCode : e.which;
		if (((k >= 48) && (k <= 57)) || k == 8 || k == 0) {
		} else {
			if (window.event) {
				window.event.returnValue = false;
			}
			else {
				e.preventDefault(); //for firefox 
			}
		}
	} 
	function checkVal(e) {
		var v = $("#"+e.target.id).val();
		if(/^0+.*/.test(v)){
			v = v.replace(new RegExp("^0+"),"");
			if (v == "" ){
				v = "1";
			}
			$("#"+e.target.id).val(v);
		}
	}
	/*-----------------*/
	
	function dropGoods(bciId){
		if (confirm('您确实要把该商品移出购物车吗？')){
			$.ajax({
				url : __contextPath + "/dropFromCart.xhtml?bciId="+bciId+"",
				type : "GET",
				cache : false,
				dataType : 'text',
				success : function(data) {
					$("#tr-"+bciId).remove();
					initTotal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
	           		alert(jqXHR.responseText);
				}
			});
		}
	}
	
	function dropAllGoods(){
		if (confirm('您确实要把清空购物车吗？')){
			$.ajax({
				url : __contextPath + "/clearCartByUserId.xhtml",
				type : "GET",
				cache : false,
				dataType : 'text',
				success : function(data) {
					$(".cartGoodsData").remove();
					initTotal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
	           		alert(jqXHR.responseText);
				}
			});
		}		
	}
	
	function changeNum(type,id){
		var num  = $("#goods_number_"+id).val();
		if (type == "add"){
			$("#goods_number_"+id).val(parseInt(num) + 1);
		}
		if (type == "sub"){
			if(parseInt(num) > 1){
				$("#goods_number_"+id).val(parseInt(num) - 1);
			}else{
				$("#goods_number_"+id).val(1);
			}
		}
		updateCartGoods(id);
	}
	
	function updateCartGoods(bciId){
		var num  = $("#goods_number_"+bciId).val();
		$.ajax({
				url : __contextPath + "/updateCartGoods.xhtml?bciId="+bciId+"&num="+num+"",
				type : "GET",
				cache : false,
				dataType : 'text',
				success : function(data) {
					var total = 0;
					var tmp = parseInt(data*100);
					total = tmp/100;
					document.getElementById("tol-"+bciId).innerHTML = "￥"+total+"元"; 
					initTotal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
	           		alert(jqXHR.responseText);
				}
			});
	}
	
	function initTotal(){
		var totalPrice = 0;
		var totalMarket = 0;
		var disparity = 0;
		var percent = 0;
		var market = document.getElementsByName("marketPrice");
		var price = document.getElementsByName("goodsPrice");
		var num = document.getElementsByName("goodsNum");
		
		for (var i = 0, j = market.length; i < j; i++){
		    totalPrice += parseFloat(price[i].innerHTML) * parseInt(num[i].value);
		    totalMarket += (parseFloat(market[i].innerHTML)) * parseInt(num[i].value);
		}
		
		totalPrice = totalPrice.toFixed(2);
		totalMarket = totalMarket.toFixed(2);
		disparity = totalMarket-totalPrice;
		disparity = disparity.toFixed(2);
		if(totalMarket != 0){
			percent = parseFloat(disparity/totalMarket).toFixed(2) * 100;
			percent = percent.toFixed(0);
		}
		document.getElementById("priceNotice").innerHTML = "购物金额小计 ￥"+totalPrice+"元，比市场价 ￥"+totalMarket+"元 节省了 ￥"+disparity+"元 ("+percent+"%)";
	}
	
	window.onload = function (){
		initTotal();
	}
</script>
[#-- 创建购物车列表自定义宏，参数：datalist 购物车列表数据(包含data，total) --]
[#macro ecs_cartlistinfo datalist]
<div class="block">
	<div class="flowBox">
		<h6>
			<span>商品列表</span>
		</h6>
		<form id="formCart" name="formCart" method="post">
			<table width="99%" align="center" border="0" cellpadding="5"
				cellspacing="1" bgcolor="#dddddd">
				<tr>
					<th bgcolor="#ffffff">
						商品名称
					</th>
					<th bgcolor="#ffffff">
						属性
					</th>
					<th bgcolor="#ffffff">
						市场价
					</th>
					<th bgcolor="#ffffff">
						本店价
					</th>
					<th bgcolor="#ffffff">
						购买数量
					</th>
					<th bgcolor="#ffffff">
						小计
					</th>
					<th bgcolor="#ffffff">
						操作
					</th>
				</tr>
				[#list datalist.data as data]
				<tr id="tr-${data.bciId}" class="cartGoodsData">
					<td bgcolor="#ffffff" align="center">
						<a href="${var_contextpath}/goods.xhtml?id=${data.bciGoods}" target="_blank" class="f6">${data.bciName}</a>
					</td>
					<td bgcolor="#ffffff">
						${data.bciAttrname}
					</td>
					<td align="center" bgcolor="#ffffff">
						￥<label name="marketPrice">${data.bciMarket+data.bciAttprice}</label>元
					</td>
					<td align="center" bgcolor="#ffffff">
						￥<label name="goodsPrice">${data.bciPrice+data.bciAttprice}</label>元
					</td>
					<td align="center" bgcolor="#ffffff">
						<div style="width:90px;overflow:hidden;marign:0 auto;">
							<img src="${path}/images/numsub.png" border="0" onclick="changeNum('sub','${data.bciId}')"
								alt="-" style="float:left;margin:4px 0 0 0;display:inline;cursor:pointer;"/>
							<input type="text" name="goodsNum" id="goods_number_${data.bciId}"
								value="${data.bciNumber?string('0')}" size="4" class="inputBg" style="text-align: center;float:left;margin: 0 5px;display:inline;"
								onchange="updateCartGoods('${data.bciId}')" maxlength="2" onkeypress="return IsNum(event)" onkeyup="checkVal(event)"/>
							<img src="${path}/images/numadd.gif" style="float:left;margin:4px 0 0 0;display:inline;cursor:pointer;" 
								border="0" alt="+" onclick="changeNum('add','${data.bciId}')" />	
						</div>	
					</td>
					<td id="tol-${data.bciId}" align="center" bgcolor="#ffffff">
						￥${data.bciTotal}元
					</td>
					<td align="center" bgcolor="#ffffff">
						<a href="javascript:dropGoods('${data.bciId}');" class="f6">删除</a>
					</td>
				</tr>
				[/#list]
			</table>
			<table width="99%" align="center" border="0" cellpadding="5"
				cellspacing="1" bgcolor="#dddddd">
				<tr>
					<td bgcolor="#ffffff">
						<label id="priceNotice"></label>
					</td>
					<td align="right" bgcolor="#ffffff">
						<input type="button" value="清空购物车" class="bnt_blue_1"
							onclick="javascript:dropAllGoods()" />
					</td>
				</tr>
			</table>
		</form>
		<table width="99%" align="center" border="0" cellpadding="5"
			cellspacing="0" bgcolor="#dddddd">
			<tr>
				<td bgcolor="#ffffff">
					<a href="${var_contextpath}/index.xhtml"><img src="${path}/images/continue.gif" alt="continue" />
					</a>
				</td>
				<td bgcolor="#ffffff" align="right">
					<a href="${var_contextpath}/order/orderDetail.xhtml"><img src="${path}/images/checkout.gif" alt="checkout" />
					</a>
				</td>
			</tr>
		</table>
	</div>
</div>
[/#macro]